/* GUI CONVERTED from rssoalla.p (converter v1.75) Sat Aug 12 23:16:44 2000 */
/* rssoalla.p - DETAIL SALES ORDER ALLOCATIONS BTB - KIT COMPONENTS          */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                       */
/* All rights reserved worldwide.  This is an unpublished work.              */
/* REVISION: 8.6     LAST MODIFIED: 11/20/96   BY: *K022* Kieu Nguyen        */
/* REVISION: 8.6     LAST MODIFIED: 05/20/98   BY: *K1Q4* Alfred Tan         */
/* REVISION: 8.6     LAST MODIFIED: 01/19/00   BY: *K254* Reetu Kapoor       */
/* REVISION: 9.1     LAST MODIFIED: 08/12/00   BY: *N0KP* myb                */
/*F0PN*/ /*V8:ConvertMode=Maintenance                                        */
/*K1Q4*/ /*V8:RunMode=Character,Windows                                      */


          /* DEFINE GLOBAL VARIABLES */
          {mfdeclre.i}

           /* THE INPUT PARAMETERS CORRESPOND WITH THE VALUES OF THE         */
           /* abs_mstr RECORD USED.                                          */
           define input parameter parent_id  like abs_id no-undo.
           define input parameter supplier_id like abs_shipfrom no-undo.
           define input parameter sod_recno as recid no-undo.
           define output parameter err_flg as logical no-undo.

           define variable this_lot     like ld_lot no-undo.
           define variable all_this_loc like sod_qty_all no-undo.
           define variable qty_to_all   like sod_qty_all no-undo.
           define variable parent_qty   like sod_qty_all no-undo.
           define variable open_qty   like sod_qty_all no-undo.

           define buffer lddet          for ld_det.


           find sod_det where recid(sod_det)  = sod_recno
           no-lock no-error.
           if not available sod_det then do:
          err_flg = yes.
              {mfmsg.i 764 3}  /* Sales Order line does not exist. */
          return.
           end.
       find so_mstr where so_nbr = sod_nbr no-lock no-error.
           if not available so_mstr then do:
          err_flg = yes.
              {mfmsg.i 609 3}  /* Sales Order does not exist. */
              return.
           end.

       open_qty = sod_qty_ord - sod_qty_pick - sod_qty_all.

           find abs_mstr where abs_shipfrom = supplier_id
                           and abs_id       = parent_id
           no-lock no-error.
       parent_qty = abs_qty.


           /*   DO ALLOCATION */
           component-loop:
           for each abs_mstr no-lock
               where abs_shipfrom = supplier_id
               and abs_par_id     = parent_id:

          find first sob_det where sob_part = abs_item and
                   sob_nbr = sod_nbr and
                   sob_line = sod_line no-lock no-error.
              if not available sob_det then next component-loop.

                 assign this_lot = ?.

                 for each lad_det where lad_dataset = "sob_det"
                                    and lad_nbr     =  sod_nbr
                                    and lad_line    =  string(sod_line)
                                    and lad_part    =  abs_item
                 no-lock:
                    accum lad_qty_all (total).
                    if this_lot = ? and lad_qty_all > 0 then this_lot = lad_lot.
                 end.


                 assign qty_to_all  =
            min ((open_qty * sob_qty_req / sod_qty_ord), abs_qty).

                 if qty_to_all > 0 then do:
                    find pt_mstr where pt_part = abs_item no-lock no-error.
                    if pt_sngl_lot = no then this_lot = ?.

                    ld_det_loop:

/*K254*/            /* UPDATING THE LD_DET FOR THE REQUIRED LOT/REF */
                    for each ld_det where  ld_site = sod_site
                                      and  ld_part = abs_item
                                      and  ld_loc  = abs_loc
/*K254*/                              and  ld_lot  = abs_lot
/*K254*/                              and  ld_ref  = abs_ref
                                      and  can-find(is_mstr
                                                    where is_status = ld_status
                                                     and is_avail  = yes      )
                                      and  ld_qty_oh - ld_qty_all > 0
                                      and (ld_expire > today or ld_expire = ?)
                    exclusive-lock:

                       if this_lot <> ? and ld_lot <> this_lot
                       then next ld_det_loop.

                       if   qty_to_all   < ld_qty_oh - ld_qty_all
                       then all_this_loc = qty_to_all.
                       else all_this_loc = ld_qty_oh - ld_qty_all.

                       if pt_sngl_lot and
                          all_this_loc < qty_to_all and this_lot = ?
                       then do for lddet:
                          for each lddet where lddet.ld_site = sod_site
                                           and lddet.ld_part = abs_item
                                           and lddet.ld_loc  = abs_loc
                                           and lddet.ld_lot  = ld_det.ld_lot
                                           and lddet.ld_ref  = ld_det.ld_ref
                                           and can-find(is_mstr
                                               where is_status = lddet.ld_status
                                                 and is_avail)
                                           and (lddet.ld_expire > today or
                                                lddet.ld_expire = ?)
                                           and (lddet.ld_qty_oh -
                                                lddet.ld_qty_all ) >= qty_to_all
                          no-lock:
                             accum (lddet.ld_qty_oh - lddet.ld_qty_all) (total).
                          end.

                          if (accum total lddet.ld_qty_oh - lddet.ld_qty_all) >=
                              qty_to_all
                          then this_lot = ld_det.ld_lot.

                       end. /* do for ld_det */

                       /* IF ALL AVAILABLE TO ALLOCATE OR NOT SINGLE LOT THEN */
                       /* CREATE LAD_DET                                      */
                       if all_this_loc = qty_to_all or pt_sngl_lot = no or
                          (this_lot <> ? and ld_lot = this_lot)
                       then do:

                          find lad_det where lad_dataset = "sob_det"
                                         and lad_nbr     =  sod_nbr
                                         and lad_line    =  string(sod_line)
                                         and lad_part    =  abs_item
                                         and lad_site    =  sod_site
                                         and lad_loc     =  abs_loc
                                         and lad_lot     =  abs_lotser
                                         and lad_ref     =  abs_ref
                          exclusive-lock no-error.
                          if not available lad_det then do:

                             create lad_det.
                             assign lad_dataset = "sob_det"
                                    lad_nbr     =  sod_nbr
                                    lad_line    =  string(sod_line)
                                    lad_site    =  sod_site
                                    lad_part    =  abs_item
                                    lad_loc     =  abs_loc
                                    lad_lot     =  abs_lotser
                                    lad_ref     =  abs_ref.
                          end.

                          assign qty_to_all  = qty_to_all  - all_this_loc
                                 ld_qty_all  = ld_qty_all  + all_this_loc
                                 lad_qty_all = lad_qty_all + all_this_loc.

                       end. /* all_this_loc = qty_to_all or pt_single_lot */

                       if qty_to_all <= 0 then leave.

                    end. /* ld_det_loop */

                    find in_mstr where in_site = sod_site and in_part = abs_item
                    exclusive-lock no-error.
                    if available in_mstr then
                in_qty_all = in_qty_all + all_this_loc .

                 end. /* qty_to_all > 0 */

           end. /* component-loop */
